﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Hanlder
{
    public class MessageHandler : IHttpHandler
    {
        #region Base
        HttpRequest Request;
        HttpResponse Response;
        System.Web.SessionState.HttpSessionState Session;
        HttpServerUtility Server;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
            methodInfo.Invoke(this, null);
        }
        public List<Model.tb_message> GetOBJList()
        {
            return Common.SqlExption.GetList<Model.tb_message>();
        }
        public void GetList()
        {
            var data = Common.SqlExption.GetList<Model.tb_message>().AsQueryable();
            if (Request["title"] != null)
            {
                string txt_search = Request["title"];
                data = data.Where(c => c.title.Contains(txt_search));
            }
            if (Request["contents"] != null)
            {
                string txt_search = Request["contents"];
                data = data.Where(c => c.contents.Contains(txt_search));
            }
            //mes_type_id
            if (Request["mes_type_id"] != null)
            {
                if (Request["mes_type_id"].Trim().Length>0)
                {
                    int mes_type_id = Convert.ToInt32(Request["mes_type_id"]);
                    data = data.Where(c => c.mes_type_id.Equals(mes_type_id));
                }
            }
            string result = JsonConvert.SerializeObject(data.ToList());
            Response.Write(result);
            Response.End();

        }
        public void Del()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = int.Parse(Request["Id"]);

            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                Common.SqlExption.Delete<Model.tb_message>(Id);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();
        }
       
        public void Edit()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = Convert.ToInt32(Request["Id"]);
            if (!int.TryParse(Request["Id"], out Id))
            {
                if (Id < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
            int mes_type_id = Convert.ToInt32(Request["mes_type_id"]);
            if (!int.TryParse(Request["mes_type_id"], out mes_type_id))
            {
                if (mes_type_id <= 0)
                {
                    rh.states = 1;
                    rh.msg = "类型验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
            var title = Request["title"];
            if (title.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "标题验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var contents = Request["contents"];
            if (contents.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "内容验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            DateTime create_time = Convert.ToDateTime(Request["create_time"]);
            if (!DateTime.TryParse(Request["create_time"], out create_time))
            {

                rh.states = 1;
                rh.msg = "时间验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();

            }
            int server_id = Convert.ToInt32(Request["server_id"]);
            if (!int.TryParse(Request["server_id"], out server_id))
            {
                if (server_id <= 0)
                {
                    rh.states = 1;
                    rh.msg = "创建人验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
            var model = Common.SqlExption.GetList<Model.tb_message>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_message();
                model.Id = Id;
                model.mes_type_id = mes_type_id;
                model.title = title;
                model.contents = contents;
                model.create_time = create_time;
                model.server_id = server_id;
                Common.SqlExption.Add(model);
            }
            else
            {
                model.Id = Id;
                model.mes_type_id = mes_type_id;
                model.title = title;
                model.contents = contents;
                model.create_time = create_time;
                model.server_id = server_id;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}
